package com.fdc_project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fdc_project.entity.Staff;
import com.fdc_project.mapper.StaffMapper;
import com.fdc_project.service.IStaffService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdc_project.vo.ErrorCode;
import com.fdc_project.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.w3c.dom.ls.LSInput;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Sk
 * @since 2021-11-29
 */
@Service
public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff> implements IStaffService {

    @Autowired
    StaffMapper staffMapper;

    @Override
    public Result add(Staff staff) {
        if (staffMapper.insert(staff) > 0) {
            return Result.success("添加成功！");
        }
        return Result.fail(-10001, "添加失败");
    }

    @Override
    public Result delete(Integer id) {
        Staff staff = staffMapper.selectById(id);
        if(staff == null || staff.getDeleted() == 1){
            return Result.fail(ErrorCode.USER_NULL.getCode(), "该员工不存在");
        }
        staff.setDeleted(1);
        if (staffMapper.updateById(staff) > 0) {
            return Result.success("员工删除成功！");
        }
        return Result.fail(-300,"删除失败！");
    }

    @Override
    public Result update(Staff staff) {
        Staff staff1 = staffMapper.selectById(staff.getStaffId());
        if(staff1 == null || staff1.getDeleted() == 1 ){
            return Result.fail(ErrorCode.USER_NULL.getCode(), "该职员不存在");
        }
        if (staffMapper.updateById(staff) > 0) {
            return Result.success("更新成功！");
        }
        return Result.fail(-300,"更新失败");
    }

    @Override
    public Result selectone(Integer id) {
        Staff staff = staffMapper.selectById(id);
        if(staff == null || staff.getDeleted() == 1 ) {
            return Result.fail(ErrorCode.USER_NULL.getCode(), "该职员不存在");
        }
        return Result.success(staff);
    }

    @Override
    public Result selectcall() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("deleted",0);
        List<Staff> staff = staffMapper.selectList(queryWrapper);
        if (staff.size() > 0) {
            return Result.success(staff);
        }
        return Result.fail(ErrorCode.USER_NULL.getCode(), "不存在职员！");
    }

    @Override
    public Result SupervisorSelection(String charge) {                            //主管选择功能
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("staff_post","主管");
        queryWrapper.eq("staff_part",charge);
        queryWrapper.eq("deleted",0);
        List list = staffMapper.selectList(queryWrapper);
        if (list.size() > 0) {
            return Result.success(list);
        }
        return Result.fail(ErrorCode.USER_NULL.getCode(), "不存在"+charge+"主管！");
    }
}
